package stdio.costa.imoca.spi;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope(BeanDefinition.SCOPE_SINGLETON)
public class ImageServiceFactory {
	
	private Map<String, ImageServiceSpi> registry = new HashMap<String, ImageServiceSpi>();
	
	@Value("#{properties['image.service']}")
	private String imageServiceKey;
	
	@Autowired
	@Qualifier("qiniuFileServiceAdapter")
	private ImageServiceSpi qiniuFileServiceAdapter;
	
	@Autowired
	@Qualifier("aliyunOssAdapter")
	private ImageServiceSpi aliyunOssAdapter;
	
	@PostConstruct
	public void postConstruct() {
		registry.put("qiniu", qiniuFileServiceAdapter);
		registry.put("aliyun", aliyunOssAdapter);
	}
	
	public ImageServiceSpi getImageService() {
		return registry.get(imageServiceKey);
	}
}
